package com.hansming.bss.modules.sfc.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.activiti.engine.impl.util.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hansming.bss.modules.sfc.entity.Bannar;
import com.hansming.bss.modules.sfc.entity.CarDatePrice;
import com.hansming.bss.modules.sfc.entity.CarInfo;
import com.hansming.bss.modules.sfc.entity.City;
import com.hansming.bss.modules.sfc.entity.Order;
import com.hansming.bss.modules.sfc.service.BannarService;
import com.hansming.bss.modules.sfc.service.CarDatePriceService;
import com.hansming.bss.modules.sfc.service.CarInfoService;
import com.hansming.bss.modules.sfc.service.CityService;
import com.hansming.bss.modules.sfc.service.OrderService;

@Controller
@RequestMapping(value = "/")
public class ForwardController {

	@Autowired
	private CityService cityService;

	@Autowired
	private BannarService bannarService;

	@Autowired
	private CarDatePriceService carDatePriceService;

	@Autowired
	private CarInfoService carInfoService;

	@Autowired
	private OrderService orderService;

	// 跳转租车首页
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		return "modules/sfc/pcsofangche/index/index/index";
	}

	// 跳转手机租车首页
	@RequestMapping(value="/mobileIndex")
	public String mobileIndex(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		
		return "modules/sfc/pcsofangche/index/index/mobleIndex";
	}

	// 跳转租车首页
	@RequestMapping(value = "/rentindex")
	public String rentindex(HttpServletRequest request, HttpServletResponse response, Model model, String rentcity,
			String startDate, String endDate, String starthour, String endhour) throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/rent/rentindex/rentindex";
	}

	// 跳转租车列表页
	@RequestMapping(value = "/rentlist")
	public String rentlist(HttpServletRequest request, HttpServletResponse response, Model model, String rentcity,
			String startDate, String endDate, String starthour, String endhour) throws UnsupportedEncodingException {

		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");

		// byte[] bytes = rentcity.getBytes("iso-8859-1");逆向操作，得到原始字符串
		// String rentcity = new String(bytes,"UTF-8");使用正确的编码转化成字符
		rentcity = new String(rentcity.getBytes("ISO-8859-1"), "UTF-8");
		rentcity = java.net.URLDecoder.decode(rentcity, "UTF-8");

		model.addAttribute("rentcity", rentcity);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		System.out.println(rentcity + "++++===+++city");
		HttpSession session = request.getSession();
		session.setAttribute("startDate", startDate);
		session.setAttribute("endDate", endDate);
		session.setAttribute("starthour", starthour);
		session.setAttribute("endhour", endhour);
		session.setAttribute("rentcity", rentcity);
		return "modules/sfc/pcsofangche/rent/rentlist/rentlist";

	}

	// 跳转租车详情页
	@RequestMapping(value = "/rentinfo")
	public String rentinfo(HttpServletRequest request, HttpServletResponse response, Model model, String rentcity,
			String startDate, String endDate, String carid, String starthour, String endhour)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		rentcity = new String(rentcity.getBytes("ISO-8859-1"), "UTF-8");
		rentcity = URLDecoder.decode(rentcity, "UTF-8");

		model.addAttribute("rentcity", rentcity);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("carid", carid);

		HttpSession session = request.getSession();
		session.setAttribute("startDate", startDate);
		session.setAttribute("endDate", endDate);
		session.setAttribute("starthour", starthour);
		session.setAttribute("endhour", endhour);
		session.setAttribute("rentcity", rentcity);
		session.setAttribute("carid", carid);

		return "modules/sfc/pcsofangche/rent/rentinfo/rentinfo";

	}

	// 跳转租车填写订单页
	@RequestMapping(value = "/rentorder")
	public String rentorder(HttpServletRequest request, HttpServletResponse response, String serviceid)
			throws UnsupportedEncodingException {
		HttpSession session = request.getSession();
		System.out.println("============================================" + serviceid);
		session.setAttribute("serviceid", serviceid);
		String token = (String) session.getAttribute("token");
		if (token != null && !"".equals(token)) {
			return "modules/sfc/pcsofangche/rent/rentorder/rentorder";
		} else {
			return loginstepone(request, response, "2");
		}

	}

	// 跳转租车支付页
	@RequestMapping(value = "/rentpay")
	public String rentpay(HttpServletRequest request, HttpServletResponse response, String rentorderid)
			throws UnsupportedEncodingException {
		HttpSession session = request.getSession();
		session.setAttribute("rentorderid", rentorderid);
		String token = (String) session.getAttribute("token");
		if (token != null && !"".equals(token)) {
			return "modules/sfc/pcsofangche/rent/rentpay/rentpay";
		} else {
			return loginstepone(request, response, "1");
		}

	}

	// 跳转微信支付页
	@RequestMapping(value = "/rentweixinpay")
	public String rentweixinpay(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/common/weixinpay/weixinpay";

	}

	// 跳转订单详情确认页
	@RequestMapping(value = "/rentrepay")
	public String rentrepay(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/rent/rentrepay/rentrepay";

	}

	// 跳转订单详情确认页
	@RequestMapping(value = "/linerepay")
	public String linerepay(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		HttpSession session = request.getSession();
		String orderId = (String) session.getAttribute("orderid");
		Order order = orderService.get(orderId);
		String orderType = order.getType();
		if (orderType.equals("1")) {
			return "modules/sfc/pcsofangche/rent/rentrepay/rentrepay";
		} else {
			return "modules/sfc/pcsofangche/line/linerepay/linerepay";
		}

	}

	// 跳转订单详情确认页
	@RequestMapping(value = "/orderlistlinerepay")
	public String orderlistlinerepay(HttpServletRequest request, HttpServletResponse response, String orderid)
			throws UnsupportedEncodingException {
		HttpSession session = request.getSession();
		session.setAttribute("orderid", orderid);
		Order order = orderService.get(orderid);
		String orderType = order.getType();
		if (orderType.equals("1")) {
			return "modules/sfc/pcsofangche/rent/rentrepay/rentrepay";
		} else {
			return "modules/sfc/pcsofangche/line/linerepay/linerepay";
		}

	}

	// 跳转线路首页
	@RequestMapping(value = "/lineindex")
	public String lineindex(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/line/lineindex/lineindex";

	}

	// 跳转租车详情页
	@RequestMapping(value = "/linelist")
	public String linelist(HttpServletRequest request, HttpServletResponse response, String linecity)
			throws UnsupportedEncodingException {
		linecity = new String(linecity.getBytes("ISO-8859-1"), "UTF-8");
		linecity = URLDecoder.decode(linecity, "UTF-8");

		HttpSession session = request.getSession();
		session.setAttribute("linecity", linecity);
		return "modules/sfc/pcsofangche/line/linelist/linelist";

	}

	// 跳转租车详情页
	@RequestMapping(value = "/lineinfo")
	public String lineinfo(HttpServletRequest request, HttpServletResponse response, String lineid, String linecity)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		lineid = new String(lineid.getBytes("ISO-8859-1"), "UTF-8");
		lineid = URLDecoder.decode(lineid, "UTF-8");

		HttpSession session = request.getSession();
		session.setAttribute("lineid", lineid);
		System.out.println(linecity + "=====" + lineid);
		session.setAttribute("linecity", linecity);

		return "modules/sfc/pcsofangche/line/lineinfo/lineinfo";

	}

	// 跳转租车详情页
	@RequestMapping(value = "/lineorder")
	public String lineorder(HttpServletRequest request, HttpServletResponse response, String lineadultnum,
			String linekidsnum, String linetime, String linecarid) throws UnsupportedEncodingException {
		lineadultnum = new String(lineadultnum.getBytes("ISO-8859-1"), "UTF-8");
		lineadultnum = URLDecoder.decode(lineadultnum, "UTF-8");
		linekidsnum = new String(linekidsnum.getBytes("ISO-8859-1"), "UTF-8");
		linekidsnum = URLDecoder.decode(linekidsnum, "UTF-8");
		linetime = new String(linetime.getBytes("ISO-8859-1"), "UTF-8");
		linetime = URLDecoder.decode(linetime, "UTF-8");
		linecarid = new String(linecarid.getBytes("ISO-8859-1"), "UTF-8");
		linecarid = URLDecoder.decode(linecarid, "UTF-8");

		HttpSession session = request.getSession();
		session.setAttribute("lineadultnum", lineadultnum);
		session.setAttribute("linekidsnum", linekidsnum);
		session.setAttribute("linetime", linetime);
		session.setAttribute("linecarid", linecarid);

		String token = (String) session.getAttribute("token");
		if (token != null && !"".equals(token)) {
			return "modules/sfc/pcsofangche/line/lineorder/lineorder";
		} else {
			return loginstepone(request, response, "4");
		}

	}

	// 跳转租车详情页
	@RequestMapping(value = "/linepay")
	public String linepay(HttpServletRequest request, HttpServletResponse response, String lineorderid)
			throws UnsupportedEncodingException {
		lineorderid = new String(lineorderid.getBytes("ISO-8859-1"), "UTF-8");
		lineorderid = URLDecoder.decode(lineorderid, "UTF-8");
		HttpSession session = request.getSession();
		session.setAttribute("lineorderid", lineorderid);
		String token = (String) session.getAttribute("token");
		if (token != null && !"".equals(token)) {
			return "modules/sfc/pcsofangche/line/linepay/linepay";
		} else {
			return loginstepone(request, response, "3");
		}

	}

	// 跳转注册页
	@RequestMapping(value = "/myregister")
	public String myregister(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/my/register/register";

	}

	// 跳转登录页
	@RequestMapping(value = "/mylogin")
	public String mylogin(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		return "modules/sfc/pcsofangche/my/login/login";

	}

	// 跳转忘记密码页
	@RequestMapping(value = "/myforget")
	public String myforget(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/my/forget/forget";

	}

	// 跳转个人中心
	@RequestMapping(value = "/mycenter")
	public String mycenter(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/my/mycenter/mycenter";

	}

	// 跳转帮助中心
	@RequestMapping(value = "/helpcenter")
	public String helpcenter(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/my/helpcenter/helpcenter";

	}

	// 跳转帮助中心
	@RequestMapping(value = "/notFound")
	public String notFound(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		return "modules/sfc/pcsofangche/common/notFound/notFound";

	}

	// 跳转超级保障
	@RequestMapping(value = "/superinsurance")
	public String superinsurance(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/my/superinsurance/superinsurance";

	}

	// 跳转租车规则页
	@RequestMapping(value = "/rentandservicerule")
	public String rentandservicerule(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/common/rentandservicerule/rentandservicerule";

	}

	// 跳转线路预定须知页
	@RequestMapping(value = "/lineservicerule")
	public String lineservicerule(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/common/lineservicerule/lineservicerule";

	}

	// 跳转线路预定须知页
	@RequestMapping(value = "/pctest")
	public String pctest(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

		return "modules/sfc/pcsofangche/common/pctest/pctest";

	}

	// 跳转线路预定须知页
	@RequestMapping(value = "/myorderlist")
	public String myorderlist(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		HttpSession session = request.getSession();
		String token = (String) session.getAttribute("token");
		if (token != null && !"".equals(token)) {
			return "modules/sfc/pcsofangche/my/myorderlist/myorderlist";
		} else {
			return loginstepone(request, response, "5");
		}

	}

	// 跳转进登录页面，第一步,跳转进登录页
	@RequestMapping(value = "/loginstepone")
	public String loginstepone(HttpServletRequest request, HttpServletResponse response, String loginstepflag)
			throws UnsupportedEncodingException {

		HttpSession session = request.getSession();
		session.setAttribute("loginstepflag", loginstepflag);
		return "modules/sfc/pcsofangche/my/login/login";

	}

	// 跳转进登录页面，第二步,登录过后跳转
	@RequestMapping(value = "/loginsteptwo")
	public String loginsteptwo(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		HttpSession session = request.getSession();
		String flag = (String) session.getAttribute("loginstepflag");
		session.setAttribute("loginstepflag", "");
		if (flag.equals("1")) {
			return "modules/sfc/pcsofangche/rent/rentpay/rentpay";
		} else if (flag.equals("2")) {
			return "modules/sfc/pcsofangche/rent/rentorder/rentorder";
		} else if (flag.equals("3")) {
			return "modules/sfc/pcsofangche/line/linepay/linepay";
		} else if (flag.equals("4")) {
			return "modules/sfc/pcsofangche/line/lineorder/lineorder";
		} else {
			return "modules/sfc/pcsofangche/index/index/index";
		}

	}
}
